class StrKeyDict0(dict):
    def __missing__(self, key):
        if isinstance(key, str):
            raise KeyError(key)
        return self[str(key)]

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __contains__(self, key):
        return key in self.keys() or str(key) in self.keys()

if __name__ == '__main__':
    print('\nTests for item retrieval using `d[key]` notation::')
    d = StrKeyDict0([('2', 'two'), ('4', 'four')])
    a = d['2']
    print(a)
    a = d[4]
    print(a)
    # a = d[1]  # 异常(打开这个注释)
    # print(a)
    print("\n\nTests for item retrieval using `d.get(key)` notation::")
    a = d.get('2')
    print(a)
    a = d.get(4)
    print(a)
    a = d.get(1, 'N/A')
    print(a)
    print('\n\nTests for the `in` operator::')
    a = 2 in d
    print(a)
    a = 1 in d
    print(a)
